<?php
class ModelCatalogGallery extends Model {
	public function addGallery($data) {
      	$this->db->query("INSERT INTO " . DB_PREFIX . "gallery SET name = '" . $this->db->escape($data['name']) . "',weblink = '" . $this->db->escape($data['weblink']) . "', sort_order = '" . (int)$data['sort_order'] . "',image = '" . $this->db->escape($data['image']) . "',type= '" . (int)$data['type'] . "' ");
		
		//$gallery_id = $this->db->getLastId();

		/*if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "gallery SET image = '" . $this->db->escape($data['image']) . "' WHERE gallery_id = '" . (int)$gallery_id . "'");
		}
		
		/*if (isset($data['gallery_store'])) {
			foreach ($data['gallery_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_to_store SET gallery_id = '" . (int)$gallery_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'gallery_id=" . (int)$gallery_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}*/
		
		$this->cache->delete('gallery');
	}
	
	public function editGallery($gallery_id, $data) {
      	$this->db->query("UPDATE " . DB_PREFIX . "gallery SET name = '" . $this->db->escape($data['name']) . "',weblink = '" . $this->db->escape($data['weblink']) . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE gallery_id = '" . (int)$gallery_id . "'");

		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "gallery SET image = '" . $this->db->escape($data['image']) . "' WHERE gallery_id = '" . (int)$gallery_id . "'");
		}
		
		/*$this->db->query("DELETE FROM " . DB_PREFIX . "gallery_to_store WHERE gallery_id = '" . (int)$gallery_id . "'");

		if (isset($data['gallery_store'])) {
			foreach ($data['gallery_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "gallery_to_store SET gallery_id = '" . (int)$gallery_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'gallery_id=" . (int)$gallery_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'gallery_id=" . (int)$gallery_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}*/
		
		$this->cache->delete('gallery');
	}
	
	public function deleteGallery($gallery_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "gallery WHERE gallery_id = '" . (int)$gallery_id . "'");
		$this->cache->delete('gallery');
	}	
	
	public function getGallery($gallery_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "gallery WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		return $query->row;
	}
	
	public function getGallerys($data = array(),$type=1) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "gallery where type=".$type." ";
			
			$sort_data = array(
				'name',
				'sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}				
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$gallery_data = $this->cache->get('gallery');
		
			if (!$gallery_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery ORDER BY name");
	
				$gallery_data = $query->rows;
			
				$this->cache->set('gallery', $gallery_data);
			}
		 
			return $gallery_data;
		}
	}

	public function getGalleryStores($gallery_id) {
		$gallery_store_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "gallery_to_store WHERE gallery_id = '" . (int)$gallery_id . "'");

		foreach ($query->rows as $result) {
			$gallery_store_data[] = $result['store_id'];
		}
		
		return $gallery_store_data;
	}
	
	public function getTotalGallerysByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "gallery WHERE image_id = '" . (int)$image_id . "'");

		return $query->row['total'];
	}

	public function getTotalGallerys($type=1) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "gallery where type=".$type);
		
		return $query->row['total'];
	}	
}
?>